package ua.com.minus1.lookbattle;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.*;
import android.widget.*;
import ua.com.minus1.lookbattle.models.CommonResult;
import ua.com.minus1.lookbattle.net.HttpManager;
import ua.com.minus1.lookbattle.net.URLManager;
import ua.com.minus1.lookbattle.utils.AppPreferences;
import ua.com.minus1.lookbattle.utils.BundleCreator;

import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: Flash
 * Date: 06.07.12
 * Time: 11:32
 * To change this template use File | Settings | File Templates.
 */
public class MainActivity extends FragmentActivity {

    private GridView gridView;
    private String uuid;
    private HttpManager httpManager;
    private TextView clicksText;
    private AppPreferences appPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        httpManager = new HttpManager(this);

        setupViews();

        appPreferences = new AppPreferences(this);
        uuid = appPreferences.getUUID();
        clicksText.setText(String.valueOf(appPreferences.getClicks()));

        //Log.i(MainActivity.class.getName(), CountriesFactory.getCountries(this).toString());

        if (uuid.length() == 0) {
            resetUuid();
            sendInitInfo();
        }
    }

    private void resetUuid() {
        uuid = UUID.randomUUID().toString();
        appPreferences.setUUID(uuid);
        appPreferences.setRegistered(false);
    }

    private void sendInitInfo() {
        Display wm = getWindowManager().getDefaultDisplay();
        httpManager.loadClass(URLManager.getStartUrl(uuid, wm.getWidth(), wm.getHeight()), CommonResult.class, new HttpManager.HttpResultHandler<CommonResult>() {
            public void onResultLoadComplete(CommonResult result) {
                Log.i(MainActivity.class.getName(), "onStart result status " + result.status);
                clicksText.setText(String.valueOf(result.clicks));
            }
        });
    }

    private void setupViews() {
        CatItemFactory.CatItem[] catItems = CatItemFactory.createItems(this);
        gridView = (GridView) findViewById(R.id.catsGridView);
        gridView.setAdapter(new CatsGridAdapter(catItems));
        gridView.setOnItemClickListener(new GridItemClickListener());

        clicksText = (TextView) findViewById(R.id.clicksText);
    }

    public void resetAppId(View view) {
        resetUuid();
        sendInitInfo();
    }

    private class CatsGridAdapter extends ArrayAdapter<CatItemFactory.CatItem>{
        private CatsGridAdapter(CatItemFactory.CatItem[] catItems) {
            super(MainActivity.this,R.layout.cat_grid_item, catItems);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.cat_grid_item, null);
            }

            CatItemFactory.CatItem catItem = getItem(position);

            TextView textView = (TextView) convertView.findViewById(R.id.catGridText);
            textView.setCompoundDrawablesWithIntrinsicBounds(null, catItem.icon, null, null);
            textView.setText(catItem.label);

            return convertView;
        }
    }


    private class GridItemClickListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            CatsGridAdapter adapter = (CatsGridAdapter) adapterView.getAdapter();

            BundleCreator creator = new BundleCreator();
            creator.writeCategory(adapter.getItem(i).catId);

            Intent intent = new Intent(MainActivity.this, BattleActivity.class);
            intent.putExtras(creator.create());
            startActivity(intent);
        }
    }
}
